package org.szy.plugin.sys.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import org.noear.solon.annotation.*;
import org.noear.solon.core.handle.ModelAndView;
import org.noear.solon.data.annotation.Tran;
import org.noear.solon.validation.annotation.NoRepeatSubmit;
import org.noear.solon.validation.annotation.NotNull;
import org.noear.solon.validation.annotation.Validated;
import org.szy.core.common.controller.BaseController;
import org.szy.core.common.entity.R;
import org.szy.core.common.param.CommonMoveParam;
import org.szy.core.common.param.CommonStatusParam;
import org.szy.plugin.sys.entity.SysMenu;
import org.szy.plugin.sys.param.SysMenuQueryParam;
import org.szy.plugin.sys.pojo.SysMenuTree;
import org.szy.plugin.sys.pojo.SysMenuTreeOption;
import org.szy.plugin.sys.service.SysMenuService;
import org.szy.plugin.sys.service.SysRoleMenuService;

import java.util.List;

/**
 * 系统菜单控制器
 *
 * @author wangbuhao
 * @since 2023-06-25 10:36:16
 */
@Controller
@Mapping("/sys/menu")
public class SysMenuController extends BaseController<SysMenu> {

    private final String PATH_PREFIX = "sys/menu/";
    @Inject
    private SysMenuService sysMenuService;
    @Inject
    private SysRoleMenuService sysRoleMenuService;

    /**
     * 系统菜单列表页面
     */
    @Get
    @Mapping("/index")
    public ModelAndView index() {
        ModelAndView view = new ModelAndView(PATH_PREFIX + "index");
        return view;
    }

    /**
     * 系统菜单编辑页
     */
    @Get
    @Mapping("/edit")
    public ModelAndView edit() {
        ModelAndView view = new ModelAndView(PATH_PREFIX + "edit");
        return view;
    }

    /**
     * 获取系统菜单树形数据
     *
     * @author wangbuhao
     * @since 2023-06-18 17:12:42
     */
    @Get
    @Mapping("/treeList")
    @SaCheckPermission(value = "sys_menu_list", mode = SaMode.OR, orRole = "wangbuhao")
    public R<List<SysMenuTree>> treeList(SysMenuQueryParam param) {
        List<SysMenu> list = sysMenuService.list(param);
        List<SysMenuTree> trees = sysMenuService.buildTrees(list);
        return R.data(trees);
    }

    /**
     * 获取菜单数据结构选项
     *
     * @param param 查询参数
     */
    @Get
    @Mapping("/treeOption")
    public R<List<SysMenuTreeOption>> treeOption(SysMenuQueryParam param) {
        List<SysMenu> list = sysMenuService.list(param);
        List<SysMenuTree> trees = sysMenuService.buildTrees(list);
        List<SysMenuTreeOption> treeOption = SysMenuTreeOption.buildTreeOption(trees);
        return R.data(treeOption);
    }

    /**
     * 获取系统菜单详情
     *
     * @author wangbuhao
     * @since 2023年6月25日10:52:03
     */
    @Get
    @Mapping("/getInfo")
    @SaCheckPermission(value = "sys_menu_detail", mode = SaMode.OR, orRole = "wangbuhao")
    public R<SysMenu> getInfo(Long id) {
        return R.asNull(sysMenuService.getById(id));
    }

    /**
     * 添加系统菜单
     *
     * @author wangbuhao
     * @since 2023年6月25日10:51:59
     */
    @Post
    @Mapping("/add")
    @NoRepeatSubmit
    @SaCheckPermission(value = "sys_menu_add", mode = SaMode.OR, orRole = "wangbuhao")
    public R<?> add(@Validated SysMenu entity) {
        boolean save = sysMenuService.add(entity);
        return save ? R.ok("保存成功", entity.getId()) : R.fail("保存失败");
    }

    /**
     * 编辑系统菜单
     *
     * @author wangbuhao
     * @since 2023年6月25日10:51:54
     */
    @Put
    @Mapping("/update")
    @NoRepeatSubmit
    @SaCheckPermission(value = "sys_menu_update", mode = SaMode.OR, orRole = "wangbuhao")
    public R<?> update(@Validated SysMenu entity) {
        boolean update = sysMenuService.edit(entity);
        return R.bool(update);
    }

    /**
     * 删除系统菜单
     *
     * @author wangbuhao
     * @since 2023年6月25日10:51:51
     */
    @Delete
    @Mapping("/delete")
    @Tran
    @SaCheckPermission(value = "sys_menu_delete", mode = SaMode.OR, orRole = "wangbuhao")
    public R<?> delete(@NotNull Long id) {
        boolean delete = sysMenuService.deleteById(id);
        if (delete) sysRoleMenuService.removeByRolId(id);
        return R.bool(delete, "删除成功", "删除失败，数据可能正在使用或已删除");
    }

    /**
     * 修改状态
     *
     * @author wangbuhao
     * @date 2023年6月25日10:51:44
     */
    @Put
    @Mapping("/modifyStatus")
    public R<?> modifyStatus(@Validated CommonStatusParam statusParam) {
        boolean status = sysMenuService.modifyStatus(statusParam);
        return R.bool(status);
    }

    /**
     * 移动菜单
     *
     * @author wangbuhao
     * @date 2023年6月25日10:51:41
     */
    @Put
    @Mapping("/move")
    public R<?> move(CommonMoveParam commonMoveParam) {
        boolean move = sysMenuService.move(commonMoveParam);
        return R.bool(move);
    }

    /**
     * 创建菜单权限
     *
     * @param id 菜单ID
     * @return 创建结果
     */
    @Put
    @Mapping("/createPerms")
    public R<?> createPerms(@NotNull Long id) {
        boolean flag = sysMenuService.createPerms(id);
        return R.bool(flag);
    }
}
